import qs from 'qs'
import { Service } from "cl-admin";
import BaseService from "@/service/base";

// 门店
@Service("shop")
class Shop extends BaseService {
	/**
	 * 全部列表
	 * @param {{biz_id?:number;bus_state?:number;parent_id?:number;keyword?:string;}} params
	 * @returns {Promise<object[]>}
	 */
	list(params) {
		return this.$get("/lists", { params });
	}
	/**
	 * 分页列表
	 * @param {{biz_id?:number;bus_state?:number;parent_id?:number;keyword?:string;page:number,size:number}} params
	 * @returns {Promise<{list:object[],pagination:object}>}
	 */
	page({ size, ...args }) {
		let params = {
			pageSize: size,
			...args
		};
		return this.$get("/lists", { params }).then(res => {
			let list = (res || []).reduce((prev, { children, ...curr }) => {
				return [...prev, ...(children || []).map(v => ({ ...v, parent: curr }))];
			}, []);
			return {
				list,
				pagination: {
					...params,
					size,
					total: list.length
				}
			};
		});
	}
	// 门店详情
	single(params) {
		return this.$get("/single", { params });
	}
	// 门店添加
	add(params) {
		return this.$post("/add", params, {
			silence: true
		});
	}
	// 门店修改
	update(params) {
		return this.$post("/edit", params, {
			silence: true
		});
	}
	// 删除，ids = [1, 2, 3]
	remove(params) {
		return this.$post("/delete", params, {
			silence: true
		});
	}
	//门店退款报表
	listrefund({ date, ...args }) {
		let params = {
			...args
		};
		if (date && date.length) {
			params.start_time = date[0];
			params.end_time = date[1];
		}
		return this.$post("/listrefund", params);
	}
	//门店退款报表
	refundShop({ bra_id = [], date, ...args }) {
		console.log('bra_id', bra_id);
		let params = {
			...args,
			bra_id: bra_id.join()
		};
		if (date && date.length) {
			params.start_time = date[0];
			params.end_time = date[1];
		}
		return this.$get("/refundShop", { params });
	}
	//部门报表
	deptach({ bra_id = [], ...args }) {
		let params = {
			...args,
			bra_id: bra_id.join()
		};
		return this.$post("/listshopdeptach", params);
	}
	// 门店报表
	shopach({ bra_id = [], ...args }) {
		let params = {
			...args,
			bra_id: bra_id.join()
		};
		return this.$post("/listshopach", params);
	}

	//个人报表
	personach({ time = [], bra_id = [], ...args }) {
		let params = {
			...args,
			bra_id: bra_id.join()
		};
		if (time && time.length) {
			params.begin_time = time[0];
			params.end_time = time[1];
		}
		return this.$post("/listshoppersonach", params);
	}
	//目标报表
	achratio({ time, bra_id = [], ...args }) {
		let params = {
			...args,
			bra_id: bra_id.join()
		};
		if (time && time.length) {
			params.begin_time = time[0];
			params.end_time = time[1];
		}
		return this.$post("/listshopachratio", params);
	}
	// 门店目标列表
	listshopplan({ size, bra_id = [], ...args }) {
		let params = {
			...args,
			pagesize: size,
			bra_id: bra_id.join()

		};
		return this.$get("/listshopplan", { params }).then(res => {
			return {
				list: res.data,
				pagination: {
					...params,
					size,
					total: res.total
				}
			};
		});
	}

	// 修改
	editshopplan(params) {
		return this.$post("/editshopplan", params);
	}

	// 删除
	delshopplan(params) {
		return this.$post("/delshopplan", params);
	}

	// 新增
	addshopplan(params) {
		return this.$post("/addshopplan", qs.stringify(params));
	}
	// 门店各品项销量表
	sales({ time = [], bra_id = [], ...args }) {
		let params = {
			bra_id: bra_id.join(),
			...args
		}
		if (time && time.length) {
			params.begin_time = time[0];
			params.end_time = time[1];
		}
		return this.$get("/sales", { params })
	}
	/**
	 * 门店每日营业老师配置
	 * @param {{shop_id:number;date:string;business_num:number}} params
	 * @returns {Promise}
	 */
	businessTeacher(params) {
		return this.$post("/businessTeacher", params)
	}
  // 预约，门店列表
	cartShop(params) {
		return this.$get("/cartshop", { params });
	}
}
export default Shop;
